How to Create a Custom Payment Gateway Plugin for Woocommerce?

Create a Custom Payment Gateway Plugin for Woocommerce

There is a clear shift from offline to online. Many factors have triggered the digital and e-commerce turning point across regions. But what are the main reasons many businesses established internet shopping in their sales process? Although there are many, some of them include:

  • Cost reduction by saving hefty amounts on rent, repairs, store design, inventory, etc. 
  • Easier to maintain an e-commerce store compared to a physical store.
  • Allows business owners to purchase a website address at an affordable price compared to opening a physical store. 

Besides these factors, what else do you think are the major elements that drove life online? In the digital era, building an online store makes it easy to grow your brand, acquire loyal customers, get creative with your marketing, and continue improvement by gaining new insights and customer feedback. 

Woocommerce Plugin

Having your website is a dynamic part of your eCommerce strategy. Woocommerce is one of the best plugins that enables you to build an eCommerce store using WordPress. It is the best and one of the most cost-efficient tools that facilitates selling your products or service on your website. 

Are you finding it difficult to create a payment gateway plugin for WooCommerce? We have got you covered! This article will help you build a payment gateway for your Woocommerce store within minutes. But before that, let’s find out how to add a payment gateway to your WordPress site. 

How to Add a Payment Gateway in WordPress?

Listed below are the series of steps you should follow to add a payment gateway to your WordPress site:

  • Purchase or download the payment gateway WordPress plugin meeting your requirements
  • Log in as a WordPress admin and click ‘Plugin’ on the sidebar menu. Doing this, you need to choose the ‘Add New’ option.
  • To upload the plugin zip file, click on ‘Upload’ and ‘Choose File.’
  • Then to begin the installation process, press ‘Ok’ and ‘Install Now.’
  • Activate your new plugin once it’s installed. 
  • In Woocommerce, go to ‘Settings’ and tap ‘Payment Methods.’
  • Considering your website needs, configure your Woocommerce payment gateway. 

Now, accessing the Woocommerce website has some in-built payment gateways. Let’s dive in to learn about them!

Default Payment Gateway in Woocommerce

If you see, the payment gateways available in Woocommerce by default are: 

1. Direct Bank Transfers (BACS)

The payments need not be made online for the Direct Bank Transfers payment gateway. 

2. Check Payment

This payment gateway does not require any payment to be made online. The payments are kept on hold until they are settled outside Woocommerce. 

3. Cash on Delivery 

In this payment mode, the order status is set to processing until the cash is paid on delivery. 

These are the default payment options to add to your wooCommerce store. However, if you want to opt for other payment gateway options, you have come to the right guide. Check out how you can create your custom payment gateway plugin for your wooCommerce site. 

Steps to Create a Payment Gateway Plugin for Woocommerce

You can add your customizable gateway to wooCommerce. Want to know how? Once you can set your custom gateway, you can set the title, description, instructions, icon, and email instructions, send additional emails, accept virtual orders, input fields to collect data on checkout, and do much more. 

Let’s begin with our tutorial to help you create a payment gateway plugin for Woocommerce: 

1. Structure of the Payment Plugin

We begin with creating a plugin. If you are not aware, customer payment methods are plugins that we need to create. 

Do you think that creating plugins for Woocommerce is hard? Once you go through these steps, you will feel like it is the easiest thing to do. To build one, you need to create a file and add lines of code inside it. 

If you see the following code, you will see payment-gateway.php file in the Plugins folder. Suppose your plugin comprises more than 1 file, move it to a folder with a similar name. For instance- tutorialswebsite-custom-payment-gateway/payment-gateway.php

Open/Edit payment-gateway.php file and copy/paste the below code

Output Screenshot:

Create Custom Payment Gateway for WooCommerce

Doing it will enable you to see the plugin in your admin area. Can you see the plugin? Yes? Great! You are all set to activate it now!

2. Build a Custom PHP Class

Payment gateways are PHP classes. You need to create a custom PHP class to extend the Woocommerce payment gateway plugin. Are you finding it challenging? We have got your back!

All you need to do is copy and paste the codes into your main plugin file payment-gateway.php as described below:

Register our custom PHP class as a custom Woocommerce Payment Gateway

Check WooCommerce plugin is activated or not

Extend the WooCommerce WC_Payment_Gateway class.

Copying and pasting the code mentioned above ‘as it is’ in your plugin file will pop a ‘500 error‘ notification on your screen. The reason is that the plugin class structure signifies the code illustrated above, where each method should be. 

3. Payment Gateway Plugin Options 

Custom payment gateway plugin options involve the following steps: 

Class Constructor (public function __construct)

  • Define class properties, like gateway ID and name lines 05-15,
  • Initialize the settings, lines 17-21,
  • Lines 23-28- Add options to class properties. 
  • Line 31- Save options
  • Line 34- If required, enqueue custom CSS and Javascript.

The payment gateway webhooks can also be registered in the class constructor on line 37. 

Option Fields (public function init_form_fields)

The option fields could be different depending on your payment processor. But in most case, it will be the same like “Enabled/Disabled”, “Title”, “Description” and “Test mode” options

After successfully implementing the above code you can see the below screenshot option, Goto -> Woocommerce ->Settings->Payments

If you have followed everything accurately, your page should appear like this. 

Custom Payment Gateway Integration

Now click on Finish set up button, and you will get the below screenshot option to set up your live and test API Key details, Enable test mode or live gateway.

Woocommerce Custom payment gateway

Fill in the payment options of your custom gateway!

Yooo, you have done 50% tasks in order to create a WooCommerce custom payment gateway.

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

4. Direct Checkout Form

Important points before implementing the code below:

  • Suppose you want to create a payment gateway on a platform similar to PayPal (a payment gateway website); you can skip step 4 [no need to add payment_fields ( ) validate_fields( )]. It means you can straightway jump to Step 5. 
  • We assume that you are using a payment processor allowing you to send card data with its own AJAX request. The payment processor you have opted for will give you a token to use in PHP, so do not add name attributes to the card form fields. 

Let’s check out how it works step by step:

1. Your customers will fill in their card details and click the ‘Place Order’ tab. 

2. Using the checkout_place_order event in Woocommere, we delay the form submission and send AJAX requests with card data to our payment processor. 

3. Submit the form in JavaScript after validating customer details. 

4. In order to process the payment, we use the PHP framework via the payment processor’s API. 

4.1 Enqueue Scripts

While you check the code in step 2, we added the wp_enqueue_scripts action hook and connected the payment_scripts ( ) method to it. 

4.2 Receive a Token in JavaScript

This code is quite different from the rest. However, the idea behind this code is the same. You can check the content for your custompayment.js file:

4.3 Payment Form with card information

Creating a payment form with card fields with payment_fields ( ) class method:

You can check the code below:

Output Screenshot of checkout payment form:

Woocommerce payment gateway

5. Payment Processing

5.1 Verify Checkout Fields

Validation of checkout fields, such as the first name, is a must. 

5.2 Set the Order Status to Recieve Payments

  • After you get the order object with wc_get_order ( ) function, you can implement methods like get_billing_address_1 ( )get_billing_name ( ), etcIt will help you fetch all the customer details. 
  • Additionally, you can add notes on Edit Order pages with the $order->add_order_note ( ) method. These notes can appear on edit order pages or member areas. 
  • What can beat if you can potentially use direct payments without going to gateway websites? Great, right? It is what we consider. 
  • Use $order->get_total ( ) to receive the order amount. 
  • So, what aids you in getting shop currency? It is get_woocommerce_currency ( )
  • However, you need to use $order->get_items ( ) if the products list is displayed as asked by your payment gateway.

5.3 Payment Gateway Callback for Instant Payment Notifications or Webhooks)

Let’s assume that our custom payment gateway does not have a form for collecting card information and that the customer will be redirected to a payment gateway website after providing his billing information (such as name, address, etc.).

How can we confirm that payment has been made and reflect it in our store?

When a customer completes an order on a payment gateway website, the gateway makes a request with $_GET parameters to a certain URL of our website that we set on the gateway’s settings page. This is how many payment gateways operate. And handling these requests is simple using WooCommerce.

The webhook URLs (callback URLs) in Woo look like this:

Let’s suppose my {webhook name} is twc_custom_payment_complete_callback. Then it will be like

Also replace {webhook name} with twc_custom_payment_complete_callback in step-3 line number 37.

And webhook() is the class method that has complete control over the received $_GET parameters.

So, this is how you create your custom payment gateway for Woocommerce websites. 

Bonus:

1. How to activate/deactivate specific payment gateways depending on a country selected on the checkout page

Let’s suppose I want to deactivate Paypal for India. So just copy and paste below code:

2. How to Get All the Installed Payment Methods 

3. How to Get Only Available Gateways  

4. How to Get Selected Payment Method 

Are you want to get implementation help, or modify or extend the functionality of this script?

A Tutorialswebsite Expert can do it for you.

Wrapping Up

A payment gateway is one of the most crucial elements of an e-commerce website. One of its primary roles is to securely route the card payment data onto the card schemes for authorization by the purchaser. 

If you want to simplify the payment process for your e-commerce business, this is how you can create a payment gateway plugin for Woocommerce effortlessly. Remember, your business success depends upon the efficiency of your payment gateway plugin. 

Good luck!

FAQs

Is it possible to create a custom payment gateway plugin for WooCommerce?

Yes, it is possible to create a WooCommerce custom payment gateway plugin.

Who can help to develop custom a WooCommerce payment gateway plugin?

A lot’s freelancers or companies can help to do it for you. You can also hire Tutorialswebsite developer team to create any WordPress or WooCommerce custom plugin.

Who is the best freelancer developer for payment gateway plugin development?

you can hire Tutorialswebsite developer team as freelancers for payment gateway plugin development.

List of Default Payment Gateway in Woocommerce

Here you can find all the default WooCommerce Payment Gateway:
1. Direct Bank Transfers
2. Check Payment
3. Cash on delivery

List of payment gateway which is best for WooCommerce?

Best WooCommerce Payment Gateway:
1. Authorize.net.
2. Braintree.
3. PayPal Pro.
4. Skrill.
5. Stripe
6. Razorpay
7. Payumoney
8. Apple Pay

List of Payment Gateways which is best in India?

The best Indian Payment Gateway is:
1. Razorpay.
2. PayU.
3. CCAvenue.
4. BillDesk.
5. Cashfree.
6. Paytm.

Related posts

One Thought to “How to Create a Custom Payment Gateway Plugin for Woocommerce?”

  1. […] How to Create a Custom Payment Gateway Plugin for Woocommerce? […]

Comments are closed.